import Notify from '@vant/weapp/notify/notify';

// 获取应用实例
const app = getApp()

Page({
  data: {
    userInfo: {},
    formOption: [
      {tip: '请输入用户名', prop: 'username', icon: 'manager'},
      {tip: '请输入密码', type: 'password', prop: 'password', icon: 'lock'},
    ],
    formState: {},
  },
  onLoad() {
  },
  getPhoneNumber (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  },
  inputChange(e) {
    let {target, detail} = e;
    let { prop } = target.dataset;
    let {formState} = this.data;
    formState[prop] = detail;
    this.setData({
      formState: {
          ...formState,
      }
    })
  },
  getUserProfile() {
    let {formState, formOption} = this.data, tip = '';
    let isBeNull = formOption.some(value => {
      if(!!!formState[value.prop]) {
        tip = value.tip;
      }
      return !!formState[value.prop];
    });
    if(isBeNull) {
      wx.getUserProfile({
        desc: '展示用户信息',
        success: (res) => {
          let {userInfo} = res;
          console.log('用户信息:' + JSON.stringify(userInfo));
          console.log('登录信息:' + JSON.stringify(formState));
          wx.showLoading({
            title: '登录中',
          });
          // 模拟请求 
          setTimeout(() => {
            wx.hideLoading();
            wx.setStorageSync('userInfo', JSON.stringify(userInfo));
            wx.setStorageSync('session', true);
            wx.showToast({
              title: '登录成功',
              icon: 'success', // error | success
            });
            app.globalData.userInfo = userInfo;
            wx.switchTab({
              url: '../home/home',
            })
          }, 1000)
        }
      })
    } else {
      Notify({ type: 'warning', message: tip, safeAreaInsetTop: true });
    }
  },
})
